rm(list = ls())

library(data.table)
library(estimatr)
library(texreg)

data_pooled <- readRDS('./replication_hasz/output/data/data_pooled.rds')
data_pooled <- data_pooled[democrat==1 | republican==1]

## OLS tables for appendix by party
data <- data_pooled[study=='Pooled']
fldata <- data_pooled[study=='Florida Study']
fdata <- data_pooled[study=='U.S. Study']

# Pooled Democrats
data[complete.cases(data[, c('college', 'male', 'white', 'latino', 'republican',
                             'strong_partisan', 'work', 'w_partner',
                             'att_mig', 'native_born', 'interest_politics' )]), complete:=1]
data[is.na(complete), complete:=0]

out1i <- lm_robust(vote_usc ~ t2 + t3 + college + male + white + latino + strong_partisan + work + w_partner + att_mig + native_born + interest_politics,
                   data=data, subset = democrat==1)
out2i <- lm_robust(vote_ownc ~ t2 + t3 + college + male + white + latino + strong_partisan + work + w_partner + att_mig + native_born + interest_politics,
                   data=data, subset = democrat==1)
out3i <- lm_robust(ballotc ~ t2 + t3 + college + male + white + latino + strong_partisan + work + w_partner + att_mig + native_born + interest_politics,
                   data=data, subset = democrat==1)
out4i <- lm_robust(more_infoc ~ t2 + t3 + college + male + white + latino + strong_partisan + work + w_partner + att_mig + native_born + interest_politics,
                   data=data, subset = democrat==1)

out1 <- lm_robust(vote_usc ~ t2 + t3,
                  data=data, subset = complete==1 & democrat==1)
out2 <- lm_robust(vote_ownc ~ t2 + t3,
                  data=data, subset = complete==1 & democrat==1)
out3 <- lm_robust(ballotc ~ t2 + t3,
                  data=data, subset = complete==1 & democrat==1)
out4 <- lm_robust(more_infoc ~ t2 + t3,
                  data=data, subset = complete==1 & democrat==1)

texreg::texreg(list(out1, out2, out3, out4, out1i, out2i, out3i, out4i),
               file = './replication_hasz/output/tables/tabD4.tex',
               stars = c(0.001,0.01,0.05),
               custom.header = list('vote US' = 1, 'vote own' = 2, 'gov dec' = 3, 'more info' = 4,
                                    'vote US' = 5, 'vote own' = 6, 'gov dec' = 7, 'more info' = 8),
               custom.model.names = c('(1)', '(2)', '(3)', '(4)', '(5)', '(6)', '(7)', '(8)'),
               custom.coef.map = list('(Intercept)' = 'control', 't2' = 'co-partisan', 't3' = 'counter-partisan',
                                      'college' = 'college', 'male' = 'male', 'white' = 'white', 'latino' = 'latino',
                                      'strong_partisan' = 'strong partisan', 'work' = 'employed',
                                      'w_partner' = 'married/partner', 'att_mig' = 'attitudes migrants',
                                      'native_born' = 'native born', 'interest_politics' = 'interest politics'),
               custom.gof.rows = list(cov = rep(c('no', 'yes'), each=4)),
               ci.force = FALSE,
               digits = 3,
               caption = "Treatment effects on support for non-citizen local voting rights among Democrats in Pooled sample",
               caption.above = TRUE,
               threeparttable = TRUE,
               include.rsquared = FALSE, include.adjrs = TRUE, include.nobs = TRUE, include.rmse = FALSE,
               label = "table:ols_pooled_dem",
               custom.note = "\\item Presents estimates of average treatment effects and heteroskedasticity-consistent 95% CIs without and with covariate adjustment for the Pooled sample (pooling together Study 1 and Study 2). The sample includes Democratic registered voters who answered covariate and outcome questions. \n %stars")

# Pooled Republicans
out1i <- lm_robust(vote_usc ~ t2 + t3 + college + male + white + latino + strong_partisan + work + w_partner + att_mig + native_born + interest_politics,
                   data=data, subset = republican==1)
out2i <- lm_robust(vote_ownc ~ t2 + t3 + college + male + white + latino + strong_partisan + work + w_partner + att_mig + native_born + interest_politics,
                   data=data, subset = republican==1)
out3i <- lm_robust(ballotc ~ t2 + t3 + college + male + white + latino + strong_partisan + work + w_partner + att_mig + native_born + interest_politics,
                   data=data, subset = republican==1)
out4i <- lm_robust(more_infoc ~ t2 + t3 + college + male + white + latino + strong_partisan + work + w_partner + att_mig + native_born + interest_politics,
                   data=data, subset = republican==1)

out1 <- lm_robust(vote_usc ~ t2 + t3,
                  data=data, subset = complete==1 & republican==1)
out2 <- lm_robust(vote_ownc ~ t2 + t3,
                  data=data, subset = complete==1 & republican==1)
out3 <- lm_robust(ballotc ~ t2 + t3,
                  data=data, subset = complete==1 & republican==1)
out4 <- lm_robust(more_infoc ~ t2 + t3,
                  data=data, subset = complete==1 & republican==1)

texreg::texreg(list(out1, out2, out3, out4, out1i, out2i, out3i, out4i),
               file = './replication_hasz/output/tables/tabD5.tex',
               stars = c(0.001,0.01,0.05),
               custom.header = list('vote US' = 1, 'vote own' = 2, 'gov dec' = 3, 'more info' = 4,
                                    'vote US' = 5, 'vote own' = 6, 'gov dec' = 7, 'more info' = 8),
               custom.model.names = c('(1)', '(2)', '(3)', '(4)', '(5)', '(6)', '(7)', '(8)'),
               custom.coef.map = list('(Intercept)' = 'control', 't2' = 'co-partisan', 't3' = 'counter-partisan',
                                      'college' = 'college', 'male' = 'male', 'white' = 'white', 'latino' = 'latino',
                                      'strong_partisan' = 'strong partisan', 'work' = 'employed',
                                      'w_partner' = 'married/partner', 'att_mig' = 'attitudes migrants',
                                      'native_born' = 'native born', 'interest_politics' = 'interest politics'),
               custom.gof.rows = list(cov = rep(c('no', 'yes'), each=4)),
               ci.force = FALSE,
               digits = 3,
               caption = "Treatment effects on support for non-citizen local voting rights among Republicans in Pooled sample",
               caption.above = TRUE,
               threeparttable = TRUE,
               include.rsquared = FALSE, include.adjrs = TRUE, include.nobs = TRUE, include.rmse = FALSE,
               label = "table:ols_pooled_rep",
               custom.note = "\\item Presents estimates of average treatment effects and heteroskedasticity-consistent 95% CIs without and with covariate adjustment for the Pooled sample (pooling together Study 1 and Study 2). The sample includes Republican registered voters who answered covariate and outcome questions. \n %stars")


# Study 1 Democrats
fldata[complete.cases(fldata[, c('college', 'male', 'white', 'latino', 'republican',
                                 'strong_partisan', 'work', 'w_partner',
                                 'att_mig', 'native_born', 'interest_politics' )]), complete:=1]
fldata[is.na(complete), complete:=0]

out1i <- lm_robust(vote_usc ~ t2 + t3 + college + male + white + latino + strong_partisan + work + w_partner + att_mig + native_born + interest_politics,
                   data=fldata, subset = democrat==1)
out2i <- lm_robust(vote_ownc ~ t2 + t3 + college + male + white + latino + strong_partisan + work + w_partner + att_mig + native_born + interest_politics,
                   data=fldata, subset = democrat==1)
out3i <- lm_robust(ballotc ~ t2 + t3 + college + male + white + latino + strong_partisan + work + w_partner + att_mig + native_born + interest_politics,
                   data=fldata, subset = democrat==1)
out4i <- lm_robust(more_infoc ~ t2 + t3 + college + male + white + latino + strong_partisan + work + w_partner + att_mig + native_born + interest_politics,
                   data=fldata, subset = democrat==1)


out1 <- lm_robust(vote_usc ~ t2 + t3,
                  data=fldata, subset = complete==1 & democrat==1)
out2 <- lm_robust(vote_ownc ~ t2 + t3,
                  data=fldata, subset = complete==1 & democrat==1)
out3 <- lm_robust(ballotc ~ t2 + t3,
                  data=fldata, subset = complete==1 & democrat==1)
out4 <- lm_robust(more_infoc ~ t2 + t3,
                  data=fldata, subset = complete==1 & democrat==1)

texreg::texreg(list(out1, out2, out3, out4, out1i, out2i, out3i, out4i),
               file = './replication_hasz/output/tables/tabD6.tex',
               stars = c(0.001,0.01,0.05),
               custom.header = list('vote US' = 1, 'vote own' = 2, 'gov dec' = 3, 'more info' = 4,
                                    'vote US' = 5, 'vote own' = 6, 'gov dec' = 7, 'more info' = 8),
               custom.model.names = c('(1)', '(2)', '(3)', '(4)', '(5)', '(6)', '(7)', '(8)'),
               custom.coef.map = list('(Intercept)' = 'control', 't2' = 'co-partisan', 't3' = 'counter-partisan',
                                      'college' = 'college', 'male' = 'male', 'white' = 'white', 'latino' = 'latino',
                                      'strong_partisan' = 'strong partisan', 'work' = 'employed',
                                      'w_partner' = 'married/partner', 'att_mig' = 'attitudes migrants',
                                      'native_born' = 'native born', 'interest_politics' = 'interest politics'),
               custom.gof.rows = list(cov = rep(c('no', 'yes'), each=4)),
               ci.force = FALSE,
               digits = 3,
               caption = "Treatment effects on support for non-citizen local voting rights among Democratic voters in Study 1",
               caption.above = TRUE,
               threeparttable = TRUE,
               include.rsquared = FALSE, include.adjrs = TRUE, include.nobs = TRUE, include.rmse = FALSE,
               label = "table:ols_study1_dem",
               custom.note = "\\item Presents estimates of average treatment effects and heteroskedasticity-consistent 95% CIs without and with covariate adjustment for Study 1. The sample includes Democratic registered voters who answered covariate and outcome questions. \n %stars")

# Study 1 Republicans
out1i <- lm_robust(vote_usc ~ t2 + t3 + college + male + white + latino + strong_partisan + work + w_partner + att_mig + native_born + interest_politics,
                   data=fldata, subset = republican==1)
out2i <- lm_robust(vote_ownc ~ t2 + t3 + college + male + white + latino + strong_partisan + work + w_partner + att_mig + native_born + interest_politics,
                   data=fldata, subset = republican==1)
out3i <- lm_robust(ballotc ~ t2 + t3 + college + male + white + latino + strong_partisan + work + w_partner + att_mig + native_born + interest_politics,
                   data=fldata, subset = republican==1)
out4i <- lm_robust(more_infoc ~ t2 + t3 + college + male + white + latino + strong_partisan + work + w_partner + att_mig + native_born + interest_politics,
                   data=fldata, subset = republican==1)


out1 <- lm_robust(vote_usc ~ t2 + t3,
                  data=fldata, subset = complete==1 & republican==1)
out2 <- lm_robust(vote_ownc ~ t2 + t3,
                  data=fldata, subset = complete==1 & republican==1)
out3 <- lm_robust(ballotc ~ t2 + t3,
                  data=fldata, subset = complete==1 & republican==1)
out4 <- lm_robust(more_infoc ~ t2 + t3,
                  data=fldata, subset = complete==1 & republican==1)

texreg::texreg(list(out1, out2, out3, out4, out1i, out2i, out3i, out4i),
               file = './replication_hasz/output/tables/tabD7.tex',
               stars = c(0.001,0.01,0.05),
               custom.header = list('vote US' = 1, 'vote own' = 2, 'gov dec' = 3, 'more info' = 4,
                                    'vote US' = 5, 'vote own' = 6, 'gov dec' = 7, 'more info' = 8),
               custom.model.names = c('(1)', '(2)', '(3)', '(4)', '(5)', '(6)', '(7)', '(8)'),
               custom.coef.map = list('(Intercept)' = 'control', 't2' = 'co-partisan', 't3' = 'counter-partisan',
                                      'college' = 'college', 'male' = 'male', 'white' = 'white', 'latino' = 'latino',
                                      'strong_partisan' = 'strong partisan', 'work' = 'employed',
                                      'w_partner' = 'married/partner', 'att_mig' = 'attitudes migrants',
                                      'native_born' = 'native born', 'interest_politics' = 'interest politics'),
               custom.gof.rows = list(cov = rep(c('no', 'yes'), each=4)),
               ci.force = FALSE,
               digits = 3,
               caption = "Treatment effects on support for non-citizen local voting rights among Republican voters in Study 1",
               caption.above = TRUE,
               threeparttable = TRUE,
               include.rsquared = FALSE, include.adjrs = TRUE, include.nobs = TRUE, include.rmse = FALSE,
               label = "table:ols_study1_rep",
               custom.note = "\\item Presents estimates of average treatment effects and heteroskedasticity-consistent 95% CIs without and with covariate adjustment for Study 1. The sample includes Republican registered voters who answered covariate and outcome questions. \n %stars")


# Study 2 Democrats
fdata[complete.cases(fdata[, c('college', 'male', 'white', 'latino', 'republican',
                               'strong_partisan', 'work', 'w_partner',
                               'att_mig', 'native_born', 'interest_politics' )]), complete:=1]
fdata[is.na(complete), complete:=0]

out1i <- lm_robust(vote_usc ~ t2 + t3 + college + male + white + latino + strong_partisan + work + w_partner + att_mig + native_born + interest_politics,
                   data=fdata, subset = democrat==1)
out2i <- lm_robust(vote_ownc ~ t2 + t3 + college + male + white + latino + strong_partisan + work + w_partner + att_mig + native_born + interest_politics,
                   data=fdata, subset = democrat==1)
out3i <- lm_robust(ballotc ~ t2 + t3 + college + male + white + latino + strong_partisan + work + w_partner + att_mig + native_born + interest_politics,
                   data=fdata, subset = democrat==1)
out4i <- lm_robust(more_infoc ~ t2 + t3 + college + male + white + latino + strong_partisan + work + w_partner + att_mig + native_born + interest_politics,
                   data=fdata, subset = democrat==1)

out1 <- lm_robust(vote_usc ~ t2 + t3,
                  data=fdata, subset = complete==1 & democrat==1)
out2 <- lm_robust(vote_ownc ~ t2 + t3,
                  data=fdata, subset = complete==1 & democrat==1)
out3 <- lm_robust(ballotc ~ t2 + t3,
                  data=fdata, subset = complete==1 & democrat==1)
out4 <- lm_robust(more_infoc ~ t2 + t3,
                  data=fdata, subset = complete==1 & democrat==1)

texreg::texreg(list(out1, out2, out3, out4, out1i, out2i, out3i, out4i),
               file = './replication_hasz/output/tables/tabD8.tex',
               stars = c(0.001,0.01,0.05),
               custom.header = list('vote US' = 1, 'vote own' = 2, 'gov dec' = 3, 'more info' = 4,
                                    'vote US' = 5, 'vote own' = 6, 'gov dec' = 7, 'more info' = 8),
               custom.model.names = c('(1)', '(2)', '(3)', '(4)', '(5)', '(6)', '(7)', '(8)'),
               custom.coef.map = list('(Intercept)' = 'control', 't2' = 'co-partisan', 't3' = 'counter-partisan',
                                      'college' = 'college', 'male' = 'male', 'white' = 'white', 'latino' = 'latino',
                                      'strong_partisan' = 'strong partisan', 'work' = 'employed',
                                      'w_partner' = 'married/partner', 'att_mig' = 'attitudes migrants',
                                      'native_born' = 'native born', 'interest_politics' = 'interest politics'),
               custom.gof.rows = list(cov = rep(c('no', 'yes'), each=4)),
               ci.force = FALSE,
               digits = 3,
               caption = "Treatment effects on support for non-citizen local voting rights among Democrats in Study 2",
               caption.above = TRUE,
               threeparttable = TRUE,
               include.rsquared = FALSE, include.adjrs = TRUE, include.nobs = TRUE, include.rmse = FALSE,
               label = "table:ols_study2_dem",
               custom.note = "\\item Presents estimates of average treatment effects and heteroskedasticity-consistent 95% CIs without and with covariate adjustment for Study 2. The sample includes Democratic registered voters who answered covariate and outcome questions. \n %stars")

# Study 2 Republicans
out1i <- lm_robust(vote_usc ~ t2 + t3 + college + male + white + latino + strong_partisan + work + w_partner + att_mig + native_born + interest_politics,
                   data=fdata, subset = republican==1)
out2i <- lm_robust(vote_ownc ~ t2 + t3 + college + male + white + latino + strong_partisan + work + w_partner + att_mig + native_born + interest_politics,
                   data=fdata, subset = republican==1)
out3i <- lm_robust(ballotc ~ t2 + t3 + college + male + white + latino + strong_partisan + work + w_partner + att_mig + native_born + interest_politics,
                   data=fdata, subset = republican==1)
out4i <- lm_robust(more_infoc ~ t2 + t3 + college + male + white + latino + strong_partisan + work + w_partner + att_mig + native_born + interest_politics,
                   data=fdata, subset = republican==1)

out1 <- lm_robust(vote_usc ~ t2 + t3,
                  data=fdata, subset = complete==1 & republican==1)
out2 <- lm_robust(vote_ownc ~ t2 + t3,
                  data=fdata, subset = complete==1 & republican==1)
out3 <- lm_robust(ballotc ~ t2 + t3,
                  data=fdata, subset = complete==1 & republican==1)
out4 <- lm_robust(more_infoc ~ t2 + t3,
                  data=fdata, subset = complete==1 & republican==1)

texreg::texreg(list(out1, out2, out3, out4, out1i, out2i, out3i, out4i),
               file = './replication_hasz/output/tables/tabD9.tex',
               stars = c(0.001,0.01,0.05),
               custom.header = list('vote US' = 1, 'vote own' = 2, 'gov dec' = 3, 'more info' = 4,
                                    'vote US' = 5, 'vote own' = 6, 'gov dec' = 7, 'more info' = 8),
               custom.model.names = c('(1)', '(2)', '(3)', '(4)', '(5)', '(6)', '(7)', '(8)'),
               custom.coef.map = list('(Intercept)' = 'control', 't2' = 'co-partisan', 't3' = 'counter-partisan',
                                      'college' = 'college', 'male' = 'male', 'white' = 'white', 'latino' = 'latino',
                                      'strong_partisan' = 'strong partisan', 'work' = 'employed',
                                      'w_partner' = 'married/partner', 'att_mig' = 'attitudes migrants',
                                      'native_born' = 'native born', 'interest_politics' = 'interest politics'),
               custom.gof.rows = list(cov = rep(c('no', 'yes'), each=4)),
               ci.force = FALSE,
               digits = 3,
               caption = "Treatment effects on support for non-citizen local voting rights among Republicans in Study 2",
               caption.above = TRUE,
               threeparttable = TRUE,
               include.rsquared = FALSE, include.adjrs = TRUE, include.nobs = TRUE, include.rmse = FALSE,
               label = "table:ols_study2_rep",
               custom.note = "\\item Presents estimates of average treatment effects and heteroskedasticity-consistent 95% CIs without and with covariate adjustment for Study 2. The sample includes Republican registered voters who answered covariate and outcome questions. \n %stars")


